package webapp.mvc.utils;

public class NumberUtils {
    public static int tryParseInt(Object obj, int defaultValue) {
        if(obj == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public static int tryParseInt(Object obj) {
        return tryParseInt(obj, 0);
    }
    public static long tryParseLong(Object obj, long defaultValue) {
        if(obj == null) {
            return defaultValue;
        }
        try {
            return Long.parseLong(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public static long tryParseLong(Object obj) {
        return tryParseLong(obj, 0L);
    }
    public static float tryParseFloat(Object obj, float defaultValue) {
        if(obj == null) {
            return defaultValue;
        }
        try {
            return Float.parseFloat(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public static float tryParseFloat(Object obj) {
        return tryParseFloat(obj, 0.0F);
    }
    public static double tryParseDouble(Object obj, double defaultValue) {
        if(obj == null) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public static double tryParseDouble(Object obj) {
        return tryParseDouble(obj, 0.0);
    }
}
